package com.suxii.frpclient.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.suxii.frpclient.R;
import com.suxii.frpclient.message.ServiceStateMessage;
import com.suxii.frpclient.thread.FrpThread;
import com.suxii.frpclient.utils.RxBus;

public class FrpService extends Service {
    private static final String CHANNEL_ID = "FrpForegroundServiceChannel";
    private static final int NOTIFICATION_ID = 1;
    private FrpThread frpThread;

    public FrpService() {
    }

    @SuppressLint("MissingPermission") // 确保权限已在 AndroidManifest 中声明
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("aa", "服务已开启");
        RxBus.getInstance().post(new ServiceStateMessage(true));

        // 创建通知通道（仅适用于Android 8.0及以上）
        createNotificationChannel();

        // 创建通知
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText("This is a foreground service.")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();

        // 启动前台服务
        startForeground(NOTIFICATION_ID, notification);

        // 模拟发送数据
       /* new Thread(() -> {
            for (int i = 0; ; i++) {
                try {
                    Thread.sleep(1000);
                    RxBus.getInstance().post("Data: " + i); // 发布数据
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt(); // 恢复中断状态
                }
            }
        }).start();*/
        // 开启任务
        frpThread = new FrpThread(getApplicationContext());
        frpThread.start();

        // 返回 START_STICKY 表示如果服务被系统杀死，应该尝试重新启动
        return START_STICKY;
    }

    private void createNotificationChannel() {
        CharSequence name = "ForegroundServiceChannel";
        String description = "Channel for Foreground Service";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);

        // 注册通知通道
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        RxBus.getInstance().post(new ServiceStateMessage(false));
        frpThread.stopThread();
        Log.e("aa", "服务已关闭");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}